// API使用示例
import { 
  jobApi, 
  applicationApi, 
  userApi, 
  verifyApi, 
  uploadApi, 
  systemApi,
  type HirePagination,
  type LaborSignupForm,
  type LaborSignupPagination,
  type AppUserForm,
  type HrAppUserVO,
  type FollowRequest
} from '@/api/index.ts'

// ==================== 招聘相关示例 ====================

/**
 * 获取招聘信息列表示例
 */
export const getJobsExample = async (params: HirePagination = {}) => {
  try {
    const result = await jobApi.getListToApp({
      currentPage: 1,
      pageSize: 10,
      type: 'progress', // progress:今日在招 ready:预招岗位
      order: 'signUpPersons', // signUpPersons:热度 workPrice:工价 location:附近
      ...params
    })
    
    if (result.code === 200) {
      console.log('获取招聘信息成功:', result.data)
      return result.data
    } else {
      console.error('获取招聘信息失败:', result.msg)
      return []
    }
  } catch (error) {
    console.error('获取招聘信息异常:', error)
    return []
  }
}

/**
 * 根据招聘ID获取详情示例
 */
export const getJobDetailExample = async (hireRecruitUseId: string) => {
  try {
    const result = await jobApi.getHireRecruitUseById(hireRecruitUseId)
    
    if (result.code === 200) {
      console.log('获取招聘详情成功:', result.data)
      return result.data
    } else {
      console.error('获取招聘详情失败:', result.msg)
      return null
    }
  } catch (error) {
    console.error('获取招聘详情异常:', error)
    return null
  }
}

/**
 * 获取所有岗位示例
 */
export const getAllPositionsExample = async () => {
  try {
    const result = await jobApi.getAllPosition()
    
    if (result.code === 200) {
      console.log('获取岗位列表成功:', result.data)
      return result.data?.list || []
    } else {
      console.error('获取岗位列表失败:', result.msg)
      return []
    }
  } catch (error) {
    console.error('获取岗位列表异常:', error)
    return []
  }
}

// ==================== 报名相关示例 ====================

/**
 * 创建报名数据示例
 */
export const createLaborSignupExample = async (applicationData: LaborSignupForm) => {
  try {
    const result = await applicationApi.createLaborSignup(applicationData)
    
    if (result.code === 200) {
      console.log('创建报名成功:', result.msg)
      return { success: true, message: result.msg }
    } else {
      console.error('创建报名失败:', result.msg)
      return { success: false, message: result.msg }
    }
  } catch (error) {
    console.error('创建报名异常:', error)
    return { success: false, message: '网络错误' }
  }
}

/**
 * 根据身份证查询报名记录示例
 */
export const getLaborSignupByIdCardExample = async (idCard: string) => {
  try {
    const result = await applicationApi.getLaborSignupByIdCard({
      currentPage: 1,
      pageSize: 10,
      F_IdCard: { value: idCard }
    })
    
    if (result.code === 200) {
      console.log('查询报名记录成功:', result.data)
      return result.data
    } else {
      console.error('查询报名记录失败:', result.msg)
      return null
    }
  } catch (error) {
    console.error('查询报名记录异常:', error)
    return null
  }
}

// ==================== 用户相关示例 ====================

/**
 * 获取用户信息示例
 */
export const getUserInfoExample = async (openId: string) => {
  try {
    const result = await userApi.getUserInfo(openId)
    
    if (result.code === 200) {
      console.log('获取用户信息成功:', result.data)
      return result.data
    } else {
      console.error('获取用户信息失败:', result.msg)
      return null
    }
  } catch (error) {
    console.error('获取用户信息异常:', error)
    return null
  }
}

/**
 * 创建用户实名信息示例
 */
export const createAppUserExample = async (userData: HrAppUserVO) => {
  try {
    const result = await userApi.createAppUser(userData)
    
    if (result.code === 200) {
      console.log('创建用户成功:', result.data)
      return { success: true, data: result.data }
    } else {
      console.error('创建用户失败:', result.msg)
      return { success: false, message: result.msg }
    }
  } catch (error) {
    console.error('创建用户异常:', error)
    return { success: false, message: '网络错误' }
  }
}

/**
 * 更新用户简历信息示例
 */
export const updateAppUserExample = async (userId: string, userData: AppUserForm) => {
  try {
    const result = await userApi.updateAppUser(userId, userData)
    
    if (result.code === 200) {
      console.log('更新用户信息成功:', result.msg)
      return { success: true, message: result.msg }
    } else {
      console.error('更新用户信息失败:', result.msg)
      return { success: false, message: result.msg }
    }
  } catch (error) {
    console.error('更新用户信息异常:', error)
    return { success: false, message: '网络错误' }
  }
}

/**
 * 关注或取消关注示例
 */
export const followJobExample = async (jobId: string, openId: string, followFlag: boolean) => {
  try {
    const followData: FollowRequest = {
      id: jobId,
      openId,
      followFlag
    }
    
    const result = await userApi.follow(followData)
    
    if (result.code === 200) {
      console.log('关注操作成功:', result.data)
      return { success: true, data: result.data }
    } else {
      console.error('关注操作失败:', result.msg)
      return { success: false, message: result.msg }
    }
  } catch (error) {
    console.error('关注操作异常:', error)
    return { success: false, message: '网络错误' }
  }
}

// ==================== 验证相关示例 ====================

/**
 * 解析身份证信息示例
 */
export const ocrIdCardExample = async (filePath: string, type: 'front' | 'back') => {
  try {
    // 注意：这里需要将文件路径转换为 File 对象
    // 在实际使用中，可能需要使用 uni.chooseImage 获取文件
    const file = await getFileFromPath(filePath)
    const result = await verifyApi.ocrIdCard(type, file)
    
    if (result.code === 200) {
      console.log('身份证解析成功:', result.data)
      return { success: true, data: result.data }
    } else {
      console.error('身份证解析失败:', result.msg)
      return { success: false, message: result.msg }
    }
  } catch (error) {
    console.error('身份证解析异常:', error)
    return { success: false, message: '网络错误' }
  }
}

// ==================== 上传相关示例 ====================

/**
 * 上传头像示例
 */
export const uploadAvatarExample = async (openId: string, filePath: string) => {
  try {
    // 注意：这里需要将文件路径转换为 File 对象
    const file = await getFileFromPath(filePath)
    const result = await uploadApi.uploadAvatar(openId, file)
    
    if (result.code === 200) {
      console.log('上传头像成功:', result.data)
      return { success: true, data: result.data }
    } else {
      console.error('上传头像失败:', result.msg)
      return { success: false, message: result.msg }
    }
  } catch (error) {
    console.error('上传头像异常:', error)
    return { success: false, message: '网络错误' }
  }
}

// ==================== 系统相关示例 ====================

/**
 * 获取数据字典示例
 */
export const getDictionaryExample = async (typeCode: string) => {
  try {
    const result = await systemApi.getListByTypeDataCode(typeCode)
    
    if (result.code === 200) {
      console.log('获取数据字典成功:', result.data)
      return result.data
    } else {
      console.error('获取数据字典失败:', result.msg)
      return []
    }
  } catch (error) {
    console.error('获取数据字典异常:', error)
    return []
  }
}

/**
 * 获取省市列表示例
 */
export const getProvincesExample = async () => {
  try {
    const result = await systemApi.getAllProByMapList()
    
    if (result.code === 200) {
      console.log('获取省市列表成功:', result.data)
      return result.data
    } else {
      console.error('获取省市列表失败:', result.msg)
      return []
    }
  } catch (error) {
    console.error('获取省市列表异常:', error)
    return []
  }
}

// ==================== 工具函数 ====================

/**
 * 从文件路径获取 File 对象
 * 注意：这是一个示例函数，实际使用时需要根据具体环境调整
 */
const getFileFromPath = async (filePath: string): Promise<File> => {
  // 在 uni-app 环境中，可能需要使用 uni.getFileSystemManager 或其他方式
  // 这里提供一个示例实现
  return new Promise((resolve, reject) => {
    // 实际实现需要根据具体环境调整
    reject(new Error('需要根据具体环境实现文件获取逻辑'))
  })
}

/**
 * 在页面中使用的 API 封装
 */
export const useApiInPage = () => {
  const getJobs = async (params: HirePagination = {}) => {
    return await getJobsExample(params)
  }
  
  const getJobDetail = async (jobId: string) => {
    return await getJobDetailExample(jobId)
  }
  
  const createApplication = async (formData: LaborSignupForm) => {
    return await createLaborSignupExample(formData)
  }
  
  const getUserInfo = async (openId: string) => {
    return await getUserInfoExample(openId)
  }
  
  const updateUserInfo = async (userId: string, userData: AppUserForm) => {
    return await updateAppUserExample(userId, userData)
  }
  
  const followJob = async (jobId: string, openId: string, followFlag: boolean) => {
    return await followJobExample(jobId, openId, followFlag)
  }
  
  const uploadFile = async (filePath: string, type: 'front' | 'back') => {
    return await ocrIdCardExample(filePath, type)
  }
  
  const uploadUserAvatar = async (openId: string, filePath: string) => {
    return await uploadAvatarExample(openId, filePath)
  }
  
  const getDictionary = async (typeCode: string) => {
    return await getDictionaryExample(typeCode)
  }
  
  const getProvinces = async () => {
    return await getProvincesExample()
  }
  
  return {
    getJobs,
    getJobDetail,
    createApplication,
    getUserInfo,
    updateUserInfo,
    followJob,
    uploadFile,
    uploadUserAvatar,
    getDictionary,
    getProvinces
  }
} 